package com.edu.service.impl;

import com.edu.StatusCode;
import com.edu.dao.EduDeptMapper;
import com.edu.dao.EduRoleMapper;
import com.edu.dao.EduTeacherMapper;
import com.edu.pojo.EduRole;
import com.edu.pojo.EduTeacher;
import com.edu.service.EduTeacherService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import tk.mybatis.mapper.entity.Example;

import java.util.Date;
import java.util.List;

/**
 * 教师服务层
 *
 * @author YJF
 */
@Service
public class EduTeacherServiceImpl implements EduTeacherService {

    @Autowired
    private EduTeacherMapper eduTeacherMapper;

    @Autowired
    private EduRoleMapper eduRoleMapper;

    @Autowired
    private EduDeptMapper eduDeptMapper;

    @Override
    public EduTeacher findUser(EduTeacher eduTeacher) {
        System.out.println(eduTeacherMapper.selectOne(eduTeacher));
        return eduTeacherMapper.selectOne(eduTeacher);
    }

    @Override
    public PageInfo<EduTeacher> findRoles(String page, String size, EduTeacher eduTeacher) {
        PageHelper.startPage(Integer.parseInt(page), Integer.parseInt(size));
        Example example = createExample(eduTeacher);
        List<EduTeacher> teacherList = eduTeacherMapper.selectByExample(example);
        EduRole tec = new EduRole();
        for (EduTeacher teacher : teacherList) {
            tec.setRoleId(teacher.getRoleId());
            teacher.setEduRole(eduRoleMapper.selectOne(tec));
        }
        return new PageInfo<>(teacherList);
    }

    @Override
    public int addUser(EduTeacher eduTeacher) {
        EduTeacher teacher = eduTeacherMapper.selectOneByExample(tecExample(eduTeacher));
        if (teacher == null) {
            return StatusCode.NOT_FIND;
        }
        if (!StringUtils.isEmpty(teacher.getTecUser())) {
            return StatusCode.REP_ERROR;
        }
        if (eduTeacherMapper.selectOneByExample(userExample(eduTeacher)) != null) {
            return StatusCode.REP_NAME;
        }
        eduTeacher.setTecId(teacher.getTecId());
        eduTeacher.setUpdateTime(new Date());
        eduTeacherMapper.updateByPrimaryKeySelective(eduTeacher);
        return StatusCode.OK;
    }

    @Override
    public PageInfo<EduTeacher> findTec(String page, String size, EduTeacher eduTeacher) {
        PageHelper.startPage(Integer.parseInt(page), Integer.parseInt(size));
        Example example = tecExample(eduTeacher);
        List<EduTeacher> teacherList = eduTeacherMapper.selectByExample(example);
        return new PageInfo<>(teacherList);
    }

    @Override
    public List<Integer> findTecByName(String curBy) {
        return eduTeacherMapper.selectIdByName(curBy);
    }

    @Override
    public EduTeacher findRoleById(Integer id) {
        return eduTeacherMapper.findRoleById(id);
    }

    @Override
    public int rePwd(Integer id) {
        int count = eduTeacherMapper.rePwd(id);
        if (count > 0) {
            return StatusCode.OK;
        }
        return StatusCode.ERROR;
    }

    @Override
    public int updateRole(EduTeacher eduTeacher) {
        return eduTeacherMapper.updateByPrimaryKeySelective(eduTeacher);
    }

    @Override
    public int setPower(EduTeacher teacher) {
        teacher.setUpdateTime(new Date());
        return eduTeacherMapper.updateByPrimaryKeySelective(teacher);
    }

    @Override
    public int delPower(EduTeacher eduTeacher) {
        eduTeacher.setUpdateTime(new Date());
        return eduTeacherMapper.delPower(eduTeacher);
    }

    @Override
    public int addTec(EduTeacher eduTeacher) {
        EduTeacher check = new EduTeacher();
        check.setTecPerId(eduTeacher.getTecPerId());
        if (eduTeacherMapper.selectOne(check) != null) {
            return StatusCode.REP_ERROR;
        }
        eduTeacher.setUpdateBy(eduTeacher.getCreateBy());
        eduTeacher.setStatus("1");
        Date date = new Date();
        eduTeacher.setUpdateTime(date);
        eduTeacher.setCreateTime(date);
        eduTeacherMapper.insert(eduTeacher);
        return StatusCode.OK;
    }

    @Override
    public EduTeacher findTecById(Long tecId) {
        EduTeacher eduTeacher = new EduTeacher();
        eduTeacher.setTecId(tecId);
        return eduTeacherMapper.selectOne(eduTeacher);
    }

    @Override
    public int updateTec(EduTeacher eduTeacher) {
        eduTeacherMapper.updateByPrimaryKeySelective(eduTeacher);
        return StatusCode.OK;
    }

    @Override
    public int delById(Long tecId) {
        eduTeacherMapper.deleteByPrimaryKey(tecId);
        eduDeptMapper.clearTecId(tecId);
        return StatusCode.OK;
    }

    @Override
    public List<EduTeacher> findAll() {
        return eduTeacherMapper.selectAll();
    }

    /**
     * 分页查询条件构建
     *
     * @param eduTeacher 查询条件
     */
    public Example createExample(EduTeacher eduTeacher) {
        Example example = new Example(EduTeacher.class);
        Example.Criteria criteria = example.createCriteria();
        if (eduTeacher != null) {
            if (!StringUtils.isEmpty(eduTeacher.getTecName())) {
                criteria.andLike("tecName", "%" + eduTeacher.getTecName() + "%");
            }
            if (!StringUtils.isEmpty(eduTeacher.getTecUser())) {
                criteria.andEqualTo("tecUser", eduTeacher.getTecUser());
            } else {
                criteria.andIsNotNull("tecUser");
                criteria.andNotLike("tecUser", "");
            }
        }
        return example;
    }

    /**
     * 查找姓名和身份证匹配的教师
     *
     * @param eduTeacher 查询条件
     */
    public Example tecExample(EduTeacher eduTeacher) {
        Example example = new Example(EduTeacher.class);
        Example.Criteria criteria = example.createCriteria();
        if (eduTeacher != null) {
            if (!StringUtils.isEmpty(eduTeacher.getTecName())) {
                criteria.andLike("tecName", "%" + eduTeacher.getTecName() + "%");
            }
            if (!StringUtils.isEmpty(eduTeacher.getTecPerId())) {
                criteria.andEqualTo("tecPerId", eduTeacher.getTecPerId());
            }
        }
        return example;
    }

    /**
     * 查找用户名是否重复
     *
     * @param eduTeacher 查询条件
     */
    public Example userExample(EduTeacher eduTeacher) {
        Example example = new Example(EduTeacher.class);
        Example.Criteria criteria = example.createCriteria();
        if (eduTeacher != null) {
            if (!StringUtils.isEmpty(eduTeacher.getTecUser())) {
                criteria.andEqualTo("tecUser", eduTeacher.getTecUser());
            }
        }
        return example;
    }
}
